package com.seocoo.worker.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.jaeger.library.StatusBarUtil;
import com.mob.MobSDK;
import com.seocoo.worker.R;
import com.seocoo.worker.base.RxQRCode;
import com.seocoo.worker.user.entity.EPromo;
import com.seocoo.worker.user.model.UserModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


/**
 * author : Ssp
 * e-mail : 981297079@qq.com
 * date   : 2020/3/3022:26
 * desc   :
 * version: 1.0
 */
public class EvaluateFriendActivity extends MVVMActivity {

    private ImageView ivCode;
    private TextView tvCode;
    private TextView tvOk;
    private SlidingTabLayout tabLayout;
    private ViewPager viwepager;

    public static void start(Context context) {
        context.startActivity(new Intent(context, EvaluateFriendActivity.class));
    }

    private final String[] mTitles = {"成功邀请工匠", "成功邀请用户"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.evaluate_friend_activity);
        View iv_back = findViewById(R.id.iv_back);
        StatusBarUtil.setTranslucentForImageView(this, 0, iv_back);
        ivCode = (ImageView) findViewById(R.id.iv_code);
        tvCode = (TextView) findViewById(R.id.tv_code);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        tabLayout = (SlidingTabLayout) findViewById(R.id.tab_layout);

        iv_back.setOnClickListener(v -> finish());
        viwepager = (ViewPager) findViewById(R.id.viwepager);

        getViewModel(UserModel.class).queryPromo();

        ArrayList<Fragment> list = new ArrayList<>();
        Fragment fragment1 = EvaluateFriendFragment.newInstance("2");
        list.add(fragment1);
        Fragment fragment2 = EvaluateFriendFragment.newInstance("1");
        list.add(fragment2);
        tabLayout.setViewPager(viwepager, mTitles, this, list);
        viwepager.setCurrentItem(0);


        MobSDK.submitPolicyGrantResult(true, null);

        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (promo == null) {
                    return;
                }
                showShare();
            }
        });
    }

    EPromo promo;

    @LiveDataMatch
    public void onPromo(EPromo promo) {
        this.promo = promo;
        if (promo != null) {
            ivCode.setImageBitmap(new RxQRCode.Builder(promo.linkUrl).into());
            tvCode.setText("邀请码：" + promo.promoCode);
        }
    }

    private void showShare() {
        ShareDialog shareDialog = new ShareDialog(this);
        shareDialog.setCancelButtonOnClickListener(v -> shareDialog.dismiss());

        shareDialog.setWxShareButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxshareLoan();
                shareDialog.dismiss();
            }
        });

        shareDialog.setWxMomentsShareButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxpyqshareLoan();
                shareDialog.dismiss();
            }
        });
    }

    //微信分享
    void wxshareLoan() {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(promo.appName + promo.title);
        sp.setTitleUrl(promo.linkUrl);
        sp.setImageUrl(promo.iconUrl);
        sp.setUrl(promo.linkUrl);
        sp.setText(promo.context);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform wx = ShareSDK.getPlatform(Wechat.NAME);
        if (!wx.isClientValid()) {
            showDialogToast("请安装微信客户端");
            return;
        }
        wx.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        // 执行图文分享
        wx.share(sp);
    }

    //微信朋友圈分享
    //微信分享
    void wxpyqshareLoan() {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(promo.appName + promo.title);
        sp.setTitleUrl(promo.linkUrl);
        sp.setImageUrl(promo.iconUrl);
        sp.setUrl(promo.linkUrl);
        sp.setText(promo.context);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform wxpyq = ShareSDK.getPlatform(WechatMoments.NAME);
        if (!wxpyq.isClientValid()) {
            showDialogToast("请安装微信客户端");
            return;
        }
        wxpyq.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        wxpyq.share(sp);
    }

}
